import loginService from "../service/loginService";
import { ElMessage } from 'element-plus';
import loginStore from "../store/loginStore";
import { useRouter } from "vue-router";

export interface IUserManage {
	User_Login: (username: string, password: string) => ovid
	getHomeHeadPortrait: (token: string) => ovid
}

export function loginHooks(): IUserManage {
	const store = loginStore();
	const router = useRouter();
	// 登录
	function User_Login(username: string, password: string) {
		//发起请求
		loginService.getLogin(username, password).then((res: any) => {
			//如果正确,存储返回来的数据
			// console.log(res.data)
			if (res.data.code == 0) {
				//存储数据
				store.setToken(res.data.data);
				ElMessage({
					type: "success",
					message: "登录成功",
				});
           sessionStorage.setItem("token",res.data.data)
				router.push("/home");
			} else {//不正确,提示
				ElMessage({
					type: "info",
					message: res.data.msg,
				});
			}
		});

	}
	// 导航显示用户头像
	function getHomeHeadPortrait(admin_token: string) {
		loginService.getHomeHeadPortrait(admin_token).then((res: any) => {
			// console.log(res.data)
			if(res.data.code==0){
				store.setHead(res.data.data);
			}
		})
	}

	return { User_Login, getHomeHeadPortrait }
}